#include <p16f1455.h>
#include "tool.h"

const unsigned char hexTableL[] = {
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

const unsigned char hexTableH[] = {
	'0', '0', '0', '0',	'0', '0', '0', '0',	'0', '0', '0', '0',	'0', '0', '0', '0',
	'1', '1', '1', '1',	'1', '1', '1', '1',	'1', '1', '1', '1',	'1', '1', '1', '1',
	'2', '2', '2', '2',	'2', '2', '2', '2',	'2', '2', '2', '2',	'2', '2', '2', '2',
	'3', '3', '3', '3',	'3', '3', '3', '3',	'3', '3', '3', '3',	'3', '3', '3', '3',
	'4', '4', '4', '4',	'4', '4', '4', '4',	'4', '4', '4', '4',	'4', '4', '4', '4',
	'5', '5', '5', '5',	'5', '5', '5', '5',	'5', '5', '5', '5',	'5', '5', '5', '5',
	'6', '6', '6', '6',	'6', '6', '6', '6',	'6', '6', '6', '6',	'6', '6', '6', '6',
	'7', '7', '7', '7',	'7', '7', '7', '7',	'7', '7', '7', '7',	'7', '7', '7', '7',
	'8', '8', '8', '8',	'8', '8', '8', '8',	'8', '8', '8', '8',	'8', '8', '8', '8',
	'9', '9', '9', '9',	'9', '9', '9', '9',	'9', '9', '9', '9',	'9', '9', '9', '9',
	'A', 'A', 'A', 'A',	'A', 'A', 'A', 'A',	'A', 'A', 'A', 'A',	'A', 'A', 'A', 'A',
	'B', 'B', 'B', 'B',	'B', 'B', 'B', 'B',	'B', 'B', 'B', 'B',	'B', 'B', 'B', 'B',
	'C', 'C', 'C', 'C',	'C', 'C', 'C', 'C',	'C', 'C', 'C', 'C',	'C', 'C', 'C', 'C',
	'D', 'D', 'D', 'D',	'D', 'D', 'D', 'D',	'D', 'D', 'D', 'D',	'D', 'D', 'D', 'D',
	'E', 'E', 'E', 'E',	'E', 'E', 'E', 'E',	'E', 'E', 'E', 'E',	'E', 'E', 'E', 'E',
	'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
};

void memset(char *p, char c, int size)
{
	while ( size-- ) *p++ = c;
}

void memcat(char *s1, char *s2)
{
	char c;

	while ( *s1 ) s1++;
	do {
		c = *s2++;
		*s1++ = c;
	} while ( c );
}

void memcpy(char *p1, char *p2, unsigned char length)
{
	while ( length-- ) *p1++ = *p2++;
}

int  strlen(char *p)
{
	int n = 0;
	while ( *p++ ) n++;
	return n;
}

void strcpy(char *d, char *s)
{
	char c;
	do {
		c = *s++;
		*d++ = c;
	} while ( c );
}

void printByte(char *buf, unsigned char c)
{
	char i = 0, buff[4];
	do {
		buff[i] = c % 10 + '0';
		i++;
		c /= 10;
	} while ( c );

	while ( i-- )
		*buf++ = buff[i];
	*buf = 0;
}

void printHex1(char *buf, unsigned char c)
{
#if 1
	*buf = hexTableL[c];
#else
	c &= 0x0f;
	if ( c > 9 ) c += 7;
	*buf = c + '0';
#endif
}

void printHex2(char *buf, unsigned char c)
{
#if 1
	char ch1 = hexTableH[c];
	char ch2 = hexTableL[c];
	FSR0 = buf;
	WREG = ch1; asm("movwf INDF0");
	WREG = ch2; asm("movwi 1[INDF0]");
#else
	unsigned char ch = c >> 4;
	if ( ch > 9 ) ch += 7;
	*buf++ = ch + '0';
	ch = c & 0x0f;
	if ( ch > 9 ) ch += 7;
	*buf   = ch + '0';
#endif
}